package com.mychat.controller;

import com.mychat.annotation.GlobalInterceptor;
import com.mychat.entity.Constants;
import com.mychat.entity.config.AppConfig;
import com.mychat.entity.dto.SysSettingDto;
import com.mychat.entity.po.GroupInfo;
import com.mychat.entity.query.GroupInfoQuery;
import com.mychat.entity.vo.PaginationResultVO;
import com.mychat.entity.vo.ResponseVO;
import com.mychat.enums.ResponseCodeEnum;
import com.mychat.exception.BusinessException;
import com.mychat.redis.RedisComponent;
import com.mychat.service.GroupInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.validation.constraints.NotEmpty;
import java.io.File;
import java.io.IOException;

@RestController
@RequestMapping("/admin")
public class AdminSettingController extends ABaseController {

    @Resource
    private RedisComponent redisComponent;
    @Resource
    private AppConfig appConfig;

    @RequestMapping("/getSysSetting")
    @GlobalInterceptor(checkAdmin = true)
    public ResponseVO getSysSetting() throws BusinessException {
        SysSettingDto sysSettingDto = redisComponent.getSysSetting();
        return getSuccessResponseVO(sysSettingDto);
    }


    @RequestMapping("/saveSysSetting")
    @GlobalInterceptor(checkAdmin = true)
    public ResponseVO saveSysSetting(SysSettingDto sysSettingDto, MultipartFile robotFile, MultipartFile robotCover) throws BusinessException, IOException {
        if(robotFile != null) {
            String baseFolder = appConfig.getProjectFolder() + Constants.FILE_FOLDER_FILE;
            File targetFileFolder =  new File(baseFolder, Constants.FILE_FOLDER_AVATAR_NAME);
            if(!targetFileFolder.exists()) {
                targetFileFolder.mkdirs();
            }
            String filePath = targetFileFolder.getPath() + "/" + Constants.ROBOT_UID ;

            robotFile.transferTo(new File(filePath + Constants.IMAGE_SUFFIX));
            robotCover.transferTo(new File(filePath + Constants.COVER_IMAGE_SUFFIX));
        }

        redisComponent.saveSysSetting(sysSettingDto);
        return getSuccessResponseVO(null);
    }

}
